In [90]:
import os
import numpy as np
from PIL import Image

In [91]:
import nibabel as nib
import scipy.misc

In [93]:
TokenName = 'Fear197ds10.nii'
img = nib.load(TokenName)

In [94]:
## Sanity check for shape
img.shape


Out[94]:
(61, 80, 1097)

In [95]:
## Convert into np array (or memmap in this case)
data = img.get_data()
print data.shape
print type(data)


(61, 80, 1097)
<class 'numpy.core.memmap.memmap'>

From documentation, 3D grayscale is organized such that we effectively have (pln, row, col),where pln is plane, row is row and column is column. Elements at the top left have the lowest brightness value,while elements in the bottom right have the highest brightness values.

I'm going to take one plane (plane 0) and then convert it into a TIFF.


In [96]:
data[0]


Out[96]:
memmap([[  0,   0,   0, ...,   0,   0,   0],
       [106,  99,  99, ..., 111, 101, 101],
       [101, 101, 100, ..., 102, 101, 101],
       ..., 
       [  0,   0,   0, ...,   0,   0,   0],
       [  0,   0,   0, ...,   0,   0,   0],
       [  0,   0,   0, ...,   0,   0,   0]], dtype=uint16)

In [97]:
plane = 0;

##Iterate through all planes to get slices
for plane in range(data.shape[0]):
    ## Convert memmap array into ndarray for toimage process
    output = np.asarray(data[plane])
    ## Save as TIFF for Ilastik
    scipy.misc.toimage(output).save('outfile' + TokenName + str(plane) + '.tiff')

In [ ]: